<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\ProductCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Tree;
use Dcat\Admin\Widgets\Box;
use Dcat\Admin\Widgets\Form as WidgetForm;
use Illuminate\Support\Facades\Storage;

class ProductCategoryController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description(trans('admin.list'))
            ->body(function (Row $row) {
                $row->column(7, $this->treeView()->render());
                $row->column(5, function (Column $column) {
                    $form = new WidgetForm();
                    $form->display('id');
                    $form->select('parent')->options(\App\Models\ProductCategory::selectOptions())->default(0);
                    $form->text('name');
                    $form->image('icon')->autoUpload();
                    $form->image('image')->autoUpload();
                    $form->text('description');
                    $form->number('sort');
                    $form->switch('status')->default(1);
                    $form->switch('is_nav')->default(1);
                    $form->width(9, 2);
                    $form->action(admin_url('product_categories'));
                    $column->append(Box::make(trans('admin.new'), $form));
                });
            });
    }

    protected function treeView()
    {
        return new Tree(new ProductCategory(), function (Tree $tree) {
            $tree->disableCreateButton();
            $tree->disableQuickCreateButton();
            $tree->disableEditButton();
            $tree->disableRefreshButton();
            $tree->maxDepth(3);
            $tree->branch(function ($branch) {
                $icon = '';
                $image = '';
                if (filled($branch['icon'])) {
                    $icon = Storage::disk('oss')->url($branch['image']);
                }
                if (filled($branch['image'])) {
                    $image = Storage::disk('oss')->url($branch['image']);
                }
                $payload = "<i class='fa {$icon}'></i>&nbsp;<strong>{$branch['name']}</strong>";
                return <<<HTML
{$payload} <img data-action="preview-img" src="{$image}" style="max-width:50px;max-height:50px;cursor:pointer" class="img img-thumbnail">
HTML;
            });
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProductCategory(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->name->tree(true);
            $grid->sort->orderable();
//            $grid->column('parent');
//            $grid->column('name');
            $grid->column('icon')->image('', 50, 50);
            $grid->column('image')->image('', 50, 50);
            $grid->column('description');
//            $grid->column('sort');
            $grid->column('status')->switch();
            $grid->column('is_nav')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['id', 'name']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProductCategory(), function (Show $show) {
            $show->field('id');
            $show->field('parent');
            $show->field('name');
            $show->field('icon');
            $show->field('image');
            $show->field('description');
            $show->field('sort');
            $show->field('status');
            $show->field('is_nav');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProductCategory(), function (Form $form) {
            $form->display('id');
            $form->disableViewButton();
            $form->disableViewCheck();
            $form->select('parent')->options(\App\Models\ProductCategory::selectOptions())->default(0);
            $form->text('name');
            $form->image('icon')->autoUpload();
            $form->image('image')->autoUpload();
            $form->text('description');
            $form->number('sort');
            $form->switch('status')->default(1);
            $form->switch('is_nav')->default(1);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
